package com.maxd.respository.helpTask;

import com.maxd.model.HelpTaskPlatform;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

public interface HelpTaskPlatformRepository extends Repository<HelpTaskPlatform, Long> {

    @Query(value = "select * from help_task_platform " +
            " order by create_time desc",
            countQuery = "select count(*) from help_task_platform " +
            " order by create_time desc",nativeQuery = true)
    Page<HelpTaskPlatform> selectHelpTaskPlatformListByState(Pageable pageable);

    @Query(value = "select * from help_task_platform where state=:state order by create_time desc",countQuery = "select count(*) from help_task_platform where state=:state",nativeQuery = true)
    Page<HelpTaskPlatform> selectHelpTaskPlatformListByState(Pageable pageable, @Param("state") Integer state);

    @Query(value = "select * from help_task_platform where classify_id=:classifyId and state=1 order by create_time desc",countQuery = "select count(*) from help_task_platform where classify_id=:classifyId and state=1",nativeQuery = true)
    Page<HelpTaskPlatform> selectHelpTaskPlatformList(Pageable pageable, @Param("classifyId") Long classifyId);

    @Transactional
    @Modifying
    @Query(value = "update help_task_platform set state=:state where id=:id",nativeQuery = true)
    int updateHelpTaskPlatformStateById(@Param("id") Long id, @Param("state") Integer state);

    @Query(value = "select p.id,p.classify_id as classifyId,p.lucky_value as luckyValue,p.title,p.title_picture as titlePicture,p.content,p.state,p.create_time as createTime " +
            "from help_task_platform p where p.id=:id ",nativeQuery = true)
    Map<String,Object> selectById(@Param("id") Long id);


}